from enum import Enum
from dataclasses import dataclass, field
from datetime import datetime
from typing import Dict, Any

class ErrorType(str, Enum):
    """错误类型分类"""
    NETWORK_ERROR = "network_error"          # 网络错误
    API_RATE_LIMIT = "api_rate_limit"        # API限流
    AUTH_ERROR = "auth_error"                # 认证错误
    QUOTA_EXCEEDED = "quota_exceeded"        # 配额超限
    SERVICE_UNAVAILABLE = "service_unavailable"  # 服务不可用
    VALIDATION_ERROR = "validation_error"    # 参数验证错误
    CONTENT_FILTER = "content_filter"        # 内容过滤
    TIMEOUT = "timeout"                      # 超时
    UNKNOWN_ERROR = "unknown_error"          # 未知错误

class RetryStrategy(str, Enum):
    """重试策略"""
    NO_RETRY = "no_retry"                    # 不重试
    IMMEDIATE = "immediate"                  # 立即重试
    LINEAR = "linear"                        # 线性退避
    EXPONENTIAL = "exponential"              # 指数退避
    ADAPTIVE = "adaptive"                    # 自适应重试

@dataclass
class RetryConfig:
    """重试配置"""
    strategy: RetryStrategy = RetryStrategy.EXPONENTIAL
    max_attempts: int = 3
    base_delay: float = 1.0  # 基础延迟秒数
    max_delay: float = 60.0  # 最大延迟秒数
    backoff_multiplier: float = 2.0
    jitter: bool = True  # 添加随机抖动
    
@dataclass
class ErrorContext:
    """错误上下文"""
    error_type: ErrorType
    original_exception: Exception
    attempt_number: int = 1
    total_attempts: int = 1
    start_time: datetime = field(default_factory=datetime.now)
    last_attempt_time: datetime = field(default_factory=datetime.now)
    additional_info: Dict[str, Any] = field(default_factory=dict)
